package app

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/jassue/jassue-gin/app/common/request"
	"github.com/jassue/jassue-gin/app/common/response"
	"github.com/jassue/jassue-gin/app/services"
)

// 添加用户
func AddUer(c *gin.Context) {
	var form request.Adduser
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	fmt.Printf("%+v\n", &form) //入库

	if err := services.UserService.AddUser(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, nil)
	}

}

// 编辑用户
func EditUer(c *gin.Context) {
	var form request.Edituser
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	fmt.Printf("%+v\n", &form) //入库

	services.UserService.EditUser(form)

	if err := services.UserService.EditUser(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, nil)
	}
}

// 启用禁用
func EditUserStatus(c *gin.Context) {
	var form request.EditStatus
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}

	fmt.Printf("%+v\n", &form) //

	if err, _ := services.UserService.EditStatus(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		response.Success(c, nil)
	}
}

// 用户列表
func GetUserList(c *gin.Context) {
	var form request.GetUserList
	if err := c.ShouldBindJSON(&form); err != nil {
		response.ValidateFail(c, request.GetErrorMsg(form, err))
		return
	}
	fmt.Printf("%+v\n", &form) //

	if err, result := services.UserService.GetUserList(form); err != nil {
		response.BusinessFail(c, err.Error())
	} else {
		fmt.Printf("%+v\n", result)
		response.Success(c, result)
	}
}
